<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/api/search";
export default {
  name: "searchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, //当前页
      per_page: 20, //当前页个数
    };
  },
  props: {
    searchText: {
      type: String,
      required: true,
    },
  },
  methods: {
    async onLoad() {
      try {
          const { data } = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.searchText,
        });
        const { results } = data.data
        this.list.push(...results)
        // console.log(this.list);
        console.log(data);
        // 设置加载状态结束
        this.loading = false;
        // 判断数据是否记载完毕 每页20条结果 第一页的数据要是加载完了 第二页的results 长度会为0
        if (results.length) {
          this.page++;
        } else {
          // 加载完毕
          this.finished = true;
        }
      } catch (error) {
        this.$toast("请求结果错误!!");
      }
    },
  },
};
</script>

<style lang="less" scoped>
</style>
